package king.dominic.jlibrary.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

import king.dominic.jlibrary.R;

/**
 * Created by Administrator on 2017/9/8.
 * RandomImageView
 */

public class RandomImageView extends AppCompatImageView {
    private int background;
    protected boolean isOpen;

    public RandomImageView(Context context) {
        this(context, null);
    }

    public RandomImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RandomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = getResources().obtainAttributes(attrs, R.styleable.RandomImageView);
        try {
            isOpen = ta.getBoolean(R.styleable.RandomImageView_rivOpen, true);
        } finally {
            ta.recycle();
        }
        background = Color.argb(255, randomColor(), randomColor(), randomColor());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (isOpen)
            canvas.drawColor(background);
        super.onDraw(canvas);
    }

    private int randomColor() {
        return (int) (255 * Math.random());
    }
}
